package com.xch.wx.controller;

import com.xch.bean.Message;
import com.xch.bean.User;
import com.xch.mvc.ResponseBody;
import com.xch.util.JSONUtil;
import com.xch.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserController {
    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        System.out.println("进入登录controller");
        String userPhone = request.getParameter("userPhone");
        String code = request.getParameter("code");
        User u = new User();
        Message msg = new Message();
        if(true){
            msg.setStatus(1);
            u.setUser(false);
        }else{
            msg.setStatus(0);
            u.setUser(true);
        }
        msg.setResult("登录成功");
        u.setUserPhone(userPhone);
        UserUtil.setWxUser(request.getSession(), u);
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = UserUtil.getWxUser(request.getSession());
        System.out.println(user);
        boolean isUser = user.isUser();
        Message msg = new Message();
        if(isUser){
            msg.setStatus(0);
        }else{
            msg.setStatus(1);
        }
        msg.setResult(user.getUserPhone());
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        //1.销毁session
        request.getSession().invalidate();
        //2. 给客户端回复消息
        Message msg = new Message(0);
        return JSONUtil.toJSON(msg);
    }
}
